﻿function DataPagingHttp() {
    var dataResult;

    var xmlHttp;
    this.createxmlHttpRequest = function () {
        if (window.ActiveXObject)
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        else if (window.XMLHttpRequest)
            xmlHttp = new XMLHttpRequest();
    };
    this.doRequestUsingGet = function (url) {
        this.createxmlHttpRequest();
        //var queryString = "/DataPaging/RequestAction.aspx?&timestamp=" + new Date().getTime();
        //queryString += actionStr;
        xmlHttp.open("GET", url);
        xmlHttp.onreadystatechange = this.handleStateChange;
        xmlHttp.send(null);
    };
    this.doRequestUsingPost = function (url, actionStr, dataResultFun) {
        dataResult = dataResultFun;
        this.createxmlHttpRequest();
        //var url = ;
        // var queryString = createQueryString();
        var queryString = actionStr;
        xmlHttp.open("POST", url);
        xmlHttp.onreadystatechange = this.handleStateChange;
        xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xmlHttp.send(queryString);
    };
    this.dataResult = function (data) {
        alert(data);
    };
    this.handleStateChange = function () {
        if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
            //alert(xmlHttp.responseText);
            dataResult(xmlHttp.responseText);
        }
    };
}

function DataPaging_Class(controlId) {
    
    var http = new DataPagingHttp();
    function DeleteTableTr(r) {
        r.style.display = "none";
        //var root = r.parentNode;
        //root.deleteRow(r.rowIndex-1);
    }
    this.DeleteTr = function(id) {
        var r = document.getElementById("tr-" + id);
        DeleteTableTr(r);
    }

    this.pageIndex = 1;
    this.orderIndex = -1;
    //this.pageSize = 10;
    //删除
    this.DeleteData = function (id, funSuccess, funFail) {
        http.doRequestUsingPost("/ARSControl/Custom.aspx?control=DataPaging&timestamp=" + new Date().getTime(), "action=del&id=" + id, function (data) {
            var json = JSON.parse(data);
            if (json.Code == 1) {
                if (funSuccess == undefined) {
                    DataPaging_Operat.DeleteTr(id);
                    //var r = document.getElementById("tr-"+id);
                    //DeleteTableTr(r);
                } else {
                    funSuccess(data);
                }
            } else {
                if (funFail == undefined) {
                    alert(json.Remark);
                } else {
                    funFail(data);
                }
            }
        });
    };

    //执行指定的sql语句
    this.ExecuteOperat = function (id, index, action) {
        http.doRequestUsingPost("/ARSControl/Custom.aspx?control=DataPaging&timestamp=" + new Date().getTime(), "action=ExecuteOperat&id=" + id + "&index=" + index + "&" + action, function (data) {
            DataPaging_Operat.ExecuteOperatCallBack(data,id);
        });
    };
    //执行execute的回调
    this.ExecuteOperatCallBack = function (data,id) {
        if (data.indexOf("Code") != -1 && data.indexOf("Remark") != -1) {
            var json = eval("(" + data + ")");
            if (json.Code == 1) {
                alert("成功");
            } else {
                alert(json.Remark);
            }
        }
    };

    //刷新的回调
    this.RefreshCallBack = function () {
        //alert(2);
    };
    this.Refresh = function(action) {
        if (this.pageIndex < 1) {
            this.pageIndex = 1;
            return;
        }
        http.doRequestUsingPost("/ARSControl/Custom.aspx?control=DataPaging&timestamp=" + new Date().getTime(), "action=getlist&PageIndex=" + this.pageIndex + "&OrderIndex=" + this.orderIndex + "&" + action, function (data) {
            //document.getElementById("datapagingDiv").innerHTML = data;
            document.getElementById(controlId+"Div").innerHTML = data;

            DataPaging_Operat.RefreshCallBack();
        });
    };

    this.Search = function (strWhere) {
        //strWhere = strWhere.replace("=", "|eq|");
        this.Refresh(strWhere);
    };
    //条件查询
    this.SearchWhere = function (strWhere) {
        //strWhere = strWhere.replace("=", "|eq|");
        this.Refresh(strWhere);
    };
    //遍历查询条件查询
    this.SearchDiv = function (strWhere) {
        this.Search(this.GetSearchWhere()+strWhere);
    };
    //转到页
    this.GoToPage = function (pageIndex) {
        if (isNaN(pageIndex)) {
            alert("页码不符合规范！");
            return;
        } else {
            this.pageIndex = pageIndex;
            if (this.pageIndex < 1) {
                this.pageIndex = 1;
                return;
            }
//            else if (this.pageIndex>={{ResultPages}}) {
//
//            }
            this.Refresh(this.GetSearchWhere());            
        }
    };
    //通过id转到页
    this.GoToPageById = function (cid) {
        var page = document.getElementById(cid).value;
        if (page != undefined && page != "") {
            this.GoToPage(parseInt(page));
        }
    };
    //下一页
    this.PageNext = function (funSuccess, funFail) {
        this.GoToPage(this.pageIndex + 1);
    };
    //上一页
    this.PagePrevious = function (funSuccess, funFail) {
        this.GoToPage(this.pageIndex - 1);
    };
    //排序index
    this.OrderIndex = function (index) {
        this.orderIndex = index;
    };

    this.GetSearchWhere = function (cssName) {
        var _results="";
        $('#DataPaging_SearchPanel .dp_search').each(function () {
            _results+=(this.name + "=" + this.value + "&");
        });
        return _results;
        //var elem, _i, _len, _ref, _results;

        //_ref = $('#DataPaging_SearchPanel .dp_search');
        //_results = [];
        //for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        //    elem = _ref[_i];
        //    _results.push(elem.id + "=" + elem.value + "&");
        //}
        //return _results;
    };
    
};
var Dp_DataPaging_ID = new DataPaging_Class('DataPaging_ID');
var DataPaging_Operat = new DataPaging_Class('DataPaging_ID');

